package com.sc.util.data;

import javax.servlet.http.HttpServletRequest;
import com.sc.dal.model.member.Member;

/**
 * 常量类
 * @author LeiJia
 * @date 2017-04-12
 */
public class SessionContainer {

	public static final String sessionID = "sessionID";//sessionIDKEY
	public static final String myCartProductCount="myCartProductCount";   //登录后的购物车
	
	
	/** cookie */
	public static final String MEMBER_INFO="memberInfo";
	

	/**
	 * @title 获取session或cookie中的登录用户对象
	 * @param request
	 * @return
	 */
	public static  Member getSessionMember(HttpServletRequest request){
		 Member m =null;
		 try{
		    String SID = request.getSession().getId();	//sesionID
		    m =  (Member) CookieUtils.getCookieEntity(request, SessionContainer.MEMBER_INFO, Member.class);
	        if(m == null){
	        	m = (Member) request.getSession().getAttribute(SessionContainer.MEMBER_INFO + SID);//根据sessionId获取登录用户
	        }
			
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 return m;
	}
	
	/**
	 * @title 获取session或cookie中的购物车数量
	 * @param request
	 * @return
	 */
	public static int getCartProductCount(HttpServletRequest request){
		int count = 0;
		String cookieCount = "";
		try{
		    String SID = request.getSession().getId();	//sesionID
		    if(CookieUtils.getCookieValue(request, SessionContainer.myCartProductCount) != null){
			    cookieCount =CookieUtils.getCookieValue(request, SessionContainer.myCartProductCount);	
		    }
	        if(Common.isEmpty(cookieCount)){
	        	if(request.getSession().getAttribute(SessionContainer.myCartProductCount + SID) != null){
		        	count = (int) request.getSession().getAttribute(SessionContainer.myCartProductCount + SID);//根据sessionId获取登录用户	        		
	        	}
	        }else{
	        	count = Integer.valueOf(cookieCount);
	        }
			
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		return count;
	}
}
